package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IMemberDao;
import com.dnzx.model.MemberBO;
import com.dnzx.model.SmsBO;
import com.dnzx.model.system.UserBO;
import com.dnzx.security.JWTUserDetails;
import com.dnzx.service.IMemberService;
import com.dnzx.service.IParamService;
import com.dnzx.service.ISmsService;
import com.dnzx.service.IUserService;

/**
 * 会员信息
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Resource
    private IMemberDao memberDao;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISmsService smsService;
    @Autowired
    private IParamService paramService;

    @Override
    public void save(MemberBO memberBO, String langCode) throws Exception {
    	SmsBO smsBO = new SmsBO();
        smsBO.setMobile(memberBO.getEmail());
        smsBO.setCode(memberBO.getCode());
        if (!this.smsService.checkCode(smsBO)) {
        	String message = this.paramService.get("VALIDATE_CODE_ERROR_" + langCode);// 验证码错误或验证码已过期
        	throw new Exception(message);
        }
        MemberBO memberBOQuery = new MemberBO();
        memberBOQuery.setEmail(memberBO.getEmail());
        MemberBO memberBODb = this.get(memberBOQuery);
        if (memberBODb != null) {
        	String message = this.paramService.get("KNOWN_USER_" + langCode);// 该用户已经存在！
        	throw new Exception(message);
        }
        
    	UserBO userBO = new UserBO();
    	userBO.setMobile(memberBO.getEmail());
    	userBO.setStatus("1");// 激活状态
    	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    	userBO.setPassword(encoder.encode(memberBO.getPassword()));
    	
    	userBO.setRoleId(3L);
    	userBO.setName(memberBO.getEmail());
    	userBO.setEmail(memberBO.getEmail());
    	this.userService.save(userBO);
    	memberBO.setUserId(userBO.getId());
    	this.memberDao.save(memberBO);
    }
 
    @Override
    public void delete(Long id) throws Exception {
        this.memberDao.delete(id);
    }

    @Override
    public void update(MemberBO memberBO) throws Exception {
        this.memberDao.update(memberBO);
    }
    
    @Override
    public MemberBO get(Long id) {
        return this.memberDao.get(id);
    }
    
    @Override
	public MemberBO get(MemberBO memberBO) {
    	List<MemberBO> memberBOList = this.memberDao.queryList(memberBO);
    	if (memberBOList != null && memberBOList.size() > 0) {
    		return memberBOList.get(0);
    	}
		return null;
	}
    
    /**
     * 获取当前登录用户的userID
     * @return
     */
    public Long getLoginUserId() {
		Authentication authen = SecurityContextHolder.getContext().getAuthentication();
		JWTUserDetails userDetails = (JWTUserDetails)authen.getPrincipal();
		return userDetails.getUserId();
	}
	
    /**
     * 获取当前登录用户的memberBO
     * @return
     */
	public MemberBO getLoginMemberBO() {
		try {
			Authentication authen = SecurityContextHolder.getContext().getAuthentication();
			JWTUserDetails userDetails = (JWTUserDetails)authen.getPrincipal();
			MemberBO memberBO = new MemberBO();
			memberBO.setUserId(userDetails.getUserId());
			return this.get(memberBO);
		} catch (Exception e) {
			return null;
		}
	}
    
    @Override
    public List<MemberBO> queryList(MemberBO memberBO) {
        return this.memberDao.queryList(memberBO);
    }
    
    @Override
    public PageResult<List<MemberBO>> queryPage(MemberBO memberBO, PageResult<List<MemberBO>> pageResult) {
    	int totalRows = this.memberDao.queryCount(memberBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(memberBO.getCurrentPage());
        pageResult.setPageSize(memberBO.getPageSize());
		List<MemberBO> records = this.memberDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), memberBO);
		pageResult.setT(records);
		return pageResult;
    }

}
